﻿using System.Collections.Generic;
using DotNetOpenAuth.OAuth;
using OrkutAPILibrary.Entities;
using Newtonsoft.Json;

namespace OrkutAPILibrary.Methods
{
    /// <summary>
    /// Orkut Profile Factory
    /// </summary>
    static class OrkutProfileFactory
    {
        const Formatting Formatting = new Formatting();

        /// <summary>
        /// Gets the self profile.
        /// </summary>
        /// <returns></returns>
        internal static OrkutPerson GetSelfProfile()
        {
            var extraData = new Dictionary<string, string>
            {
				{ "method", "people.get" },
				{ "userId", "@me" },
				{ "groupId", "@self" },
				{ "fields", "id,profileUrl, thumbnailUrl, displayName, name, dateOfBirth, languagesSpoken, gender, aboutMe, bodyType, currentLocation, fashion, humor, livingArrangement, lookingFor, relationshipStatus, religion, politicalViews, romance, status, urls, interests, sports, activities, books, music, tvShows, movies, food, emails, phoneNumbers, turnOns, turnOffs" },
			};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                ? null
                : JsonConvert.DeserializeObject<OrkutPerson>(requestResponse);
        }

        /// <summary>
        /// Gets the profile of the requested user id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        internal static OrkutPerson GetProfileOfUser(string userId)
        {
            var extraData = new Dictionary<string, string>
            {
				{ "method", "people.get" },
				{ "userId", userId },
				{ "groupId", "@self" },
				{ "fields", "id, profileUrl, thumbnailUrl, displayName, name, dateOfBirth, languagesSpoken, gender, aboutMe, bodyType, currentLocation, fashion, humor, livingArrangement, lookingFor, relationshipStatus, religion, politicalViews, romance, status, urls, interests, sports, activities, books, music, tvShows, movies, food, emails, phoneNumbers, turnOns, turnOffs" },
			};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                ? null
                : JsonConvert.DeserializeObject<OrkutPerson>(requestResponse);
        }

        /// <summary>
        /// Updates the self profile.
        /// </summary>
        /// <param name="profileToUpdate">The profile fields to update.</param>
        /// <returns></returns>
        internal static bool UpdateSelfProfile(OrkutPerson profileToUpdate)
        {
            var set = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };

            var extraData = new Dictionary<string, string>
            {
				{ "method", "people.get" },
				{ "userId", "@me" },
				{ "groupId", "@self" },
				{ "person",  JsonConvert.SerializeObject(profileToUpdate, Formatting, set)},
			};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                ? true
                : false;
        }
    }
}
